package nsn.message.aggregator.model.provider;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import nsn.message.aggregator.model.AbstractMessageAggregator;
import nsn.message.aggregator.model.IMessageAggregator;
import nsn.message.aggregator.model.ISourceType;
import nsn.message.aggregator.model.provider.exceptions.ModelProviderException;


import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
/**
 * 
 *
 * @author Lukasz Nowak
 * @since 10-03-2011
 *
 */
public class MessageAggregatorProvider extends AbstractMessageAggregator {

    /**
	 */
	private static final String SOURCE_TYPE = "sourceType";

	static private MessageAggregatorProvider instance;

    static private String ID = "nsn.message.aggregator.model.source_type";

    /**
     * 
     */
    static private  final long serialVersionUID = -8165446537095172914L;

    /**
     * 
     *
     * @return Model
     */
    static public IMessageAggregator getInstance() {
        if (instance == null) {
            instance = new MessageAggregatorProvider();
            instance.restore();
        }
        return instance;
    }

    @Override
    public void restore() {
    	initSourceTypes();      
    }

	private void initSourceTypes() {
		final IConfigurationElement[] config = Platform.getExtensionRegistry()
                .getConfigurationElementsFor(ID);
        try {
            for (final IConfigurationElement configElement : config) {
                final Object obj = configElement
                        .createExecutableExtension(SOURCE_TYPE);
                if (obj instanceof ISourceType) {
                	final ISourceType type = (ISourceType) obj;
                	type.addPropertyChangeListener(new PropertyChangeListener() {
						
						@Override
						public void propertyChange(final PropertyChangeEvent event) {
							firePropertyChange(event);
						}
					});
                    getSourceTypes().add(type);
                }

            }
        } catch (final CoreException ex) {
        	throw new ModelProviderException(ex);
        }
	}


	@Override
	public void store() {
		// TODO Auto-generated method stub
	}

}
